package com.iretailer.report.util;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by xjw on 2017/8/25.
 */
public class TimeUtilOld {

    //锁对象
    private static final Object lockObj = new Object();
    private static Map<String, ThreadLocal<SimpleDateFormat>> sdfMap = new HashMap<>();

    private static SimpleDateFormat getSdf(final String pattern) {
        ThreadLocal<SimpleDateFormat> tl = sdfMap.get(pattern);
        //此处双重判断和同步是为了防止 sdfMap这个单例多次put重复的 sdf
        if (tl == null) {
            synchronized (lockObj) {
                tl = sdfMap.get(pattern);
                if (tl == null) {
                    tl = new ThreadLocal<SimpleDateFormat>() {
                        @Override
                        protected SimpleDateFormat initialValue() {
                            return new SimpleDateFormat(pattern);
                        }
                    };
                    sdfMap.put(pattern,tl);
                }
            }
        }
        return tl.get();
    }

//    private static SimpleDateFormat sdf_y = new SimpleDateFormat("yyyy-MM-dd");
//    private static SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
//    private static SimpleDateFormat sdf_ = new SimpleDateFormat("yyyy-MM-dd HH:mm");
//    private static SimpleDateFormat sdf_s = new SimpleDateFormat("HH:mm:ss");

    /**
     * 给定某一格式的字符串 返回 日期
     *
     * @param date yyy-MM-dd 或者  yyy-MM-dd HH:mm:ss
     * @return
     */
    public static Date getDateOfString(String date) {

        Date result = null;
        try {
            if (StringUtils.isNotEmpty(date)) {
                if (date.indexOf(":") > -1) {

                    if (date.length() > 16) {
                        result = getSdf("yyyy-MM-dd HH:mm:ss").parse(date); //19
                    } else {
                        result = getSdf("yyyy-MM-dd HH:mm").parse(date); //16
                    }
                } else {
                    result = getSdf("yyyy-MM-dd").parse(date);
                }

            } else {
                result = new Date();
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 根据format
     * @param date
     * @param format
     * @return
     */
    public static Date getDateOfString(String date,String format) {

        Date result = null;
        try {
            if (StringUtils.isNotEmpty(date)) {

                    result = getSdf(format).parse(date);
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return result;
    }


    /**
     * 获得本月的 1号
     * @param date
     * @return
     */
    public static String getMonthFirstDay(String date){
        Date date1 = getDateOfString(date);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date1);
        calendar.set(Calendar.DAY_OF_MONTH,1);
        return getDate(calendar.getTime());

    }

    /**
     * 给定某一个格式的字符串，返回星期几
     *
     * @param date yyy-MM-dd
     * @return
     */

    public static String getWeekOfString(String date) {
        String result = null;
        try {
            Date date1 = getSdf("yyyy-MM-dd").parse(date);
            String[] weekDays = {"日", "一", "二", "三", "四", "五", "六"};
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date1);
            int w = calendar.get(Calendar.DAY_OF_WEEK) - 1;
            if (w < 0)
                w = 0;
            result = weekDays[w];
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return result;
    }

    public static int getWeek(String date){
        Date date1 = getDateOfString(date);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date1);
        int w = calendar.get(Calendar.DAY_OF_WEEK) - 1;
        if (w < 0)
            w = 0;

        return  w;
    }


    /**
     * 返回  yyy-MM-dd
     *
     * @param date
     * @return
     */
    public static String getDate(Date date) {
        return getSdf("yyyy-MM-dd").format(date);
    }

    /**
     * 返回  HH:mm:ss
     *
     * @param date
     * @return
     */
    public static String getTime(Date date) {
        return getSdf("HH:mm:ss").format(date);
    }

    /**
     * 返回  yyy-MM-dd HH:mm:ss
     *
     * @param date
     * @return
     */
    public static String getDateTime(Date date) {
        return getSdf("yyy-MM-dd HH:mm:ss").format(date);
    }

    /**
     * 返回  yyy-MM-dd HH:mm:ss
     *
     * @param date
     * @return
     */
    public static String getByFormat(Date date,String format) {
        return getSdf(format).format(date);
    }

    /**
     * utc 时间转为本地时间
     *
     * @param dateTime    utc 时间
     * @param utcFormat
     * @param localFormat
     * @return
     */
    public static String utc2Local(String dateTime, String utcFormat, String localFormat) {

        String reuslt = null;
        try {
            if (utcFormat == null) {
                utcFormat = "yyyy-MM-dd'T'HH:mm:ss'Z'";
            }
            if (localFormat == null) {
                localFormat = "yyyy-MM-dd HH:mm:ss";
            }

            SimpleDateFormat sdf_utc = new SimpleDateFormat(utcFormat);
            SimpleDateFormat sdf_local = new SimpleDateFormat(localFormat);
            sdf_utc.setTimeZone(TimeZone.getTimeZone("UTC"));
            Date date = sdf_utc.parse(dateTime);
            reuslt = sdf_local.format(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return reuslt;

    }

    /**
     * 本地时间转换为 UTC 时间
     *
     * @param date
     * @param utcFormat
     * @return
     */

    public static String local2Utc(Date date, String utcFormat) {
        String reuslt = null;
        try {
            if (utcFormat == null) {
                utcFormat = "yyyy-MM-dd'T'HH:mm:ss'Z'";
            }
            SimpleDateFormat sdf_utc = new SimpleDateFormat(utcFormat);
            sdf_utc.setTimeZone(TimeZone.getTimeZone("UTC"));
            reuslt = sdf_utc.format(date);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return reuslt;

    }

    /**
     * 两个时间相比较
     * 如果等于1 first大于 second
     * 如果等于 0 两个时相等
     * 如果等于 -1 first 小于 second
     *
     * @param first
     * @param second
     * @return
     */
    public static int compare(String first, String second) {
        Long firstDate = getDateOfString(first).getTime();
        Long secondDate = getDateOfString(second).getTime();
        int result = -1;
        if (firstDate > secondDate) {
            result = 1;
        } else if (firstDate < secondDate) {
            result = -1;
        } else {
            result = 0;
        }

        return result;
    }
    /**
     * 两个时间相比较
     * 如果等于1 first大于 second
     * 如果等于 0 两个时相等
     * 如果等于 -1 first 小于 second
     *
     * @param first
     * @param second
     * @return
     */
    public static int compare(Date first, Date second) {
        Long firstDate = first.getTime();
        Long secondDate = second.getTime();
        int result = -1;
        if (firstDate > secondDate) {
            result = 1;
        } else if (firstDate < secondDate) {
            result = -1;
        } else {
            result = 0;
        }

        return result;
    }

    /**
     * date1 和 date 2相差几天、小时、分钟
     *
     * @param date1
     * @param date2
     * @return
     */
    public static int diffDays(Date date1, Date date2, String type) {
        long d1 = date1.getTime();
        long d2 = date2.getTime();
        if (type.equalsIgnoreCase("day")) {
            long a = (d1 - d2) / (24 * 60 * 60 * 1000);
            return (int) a;
        } else if (type.equalsIgnoreCase("hour")) {
            long a = (d1 - d2) / (60 * 60 * 1000);
            return (int) a;
        } else if (type.equalsIgnoreCase("minute")) {
            long a = (d1 - d2) / (60 * 1000);
            return (int) a;
        }
        return 0;
    }

    /**
     * 得到两个时间段中的所有日期
     * @param startDate
     * @param endDate
     * @return
     */
    public static List<String> separate(String startDate,String endDate){
        Date d1 = getDateOfString(startDate);
        Date d2 = getDateOfString(endDate);
        int a = diffDays(d2,d1,"day");
        List<String> result = new ArrayList<>();
            for(int i = 0;i<a+1;i++){
                result.add(getSpecifiledDay(d1,i,"yyyy-MM-dd"));
            }

        return result;

    }

    /**
     *
     * @param date
     * @param n
     * @return
     */
    public static String getSpecifiledMonth(Date date, int n){

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.MONTH,n);
        return getSdf("yyyy-MM-dd HH:mm:ss").format(calendar.getTime());
    }



    /**
     * 传入的 n 为 正就是date 的后d天， 为负就是date的前d天
     *
     * @param date
     * @param n
     * @return
     */

    public static String getSpecifiledDay(Date date, int n, String format) {
        long d = date.getTime();
        long day = 24 * 60 * 60 * 1000;
        long result = d + day * n;
        SimpleDateFormat sdfg = null;
        if (format != null) {
            sdfg = getSdf(format);
        } else {
            sdfg = getSdf("yyyy-MM-dd HH:mm:ss");
        }

        return sdfg.format(result);

    }

    public static Date getSpecifiledDay(Date date, int n) {
        long d = date.getTime();
        long day = 24 * 60 * 60 * 1000;
        long result = d + day * n;

        return new Date(result);
    }

    public static Date getSpecifiledMinutes(Date date, int n) {
        long d = date.getTime();
        long minutes = 60 * 1000;
        long result = d + minutes * n;

        return new Date(result);
    }

    /**
     * AM 和 PM 时间转换
     * @param time
     * @return
     */
    public static String AMPM(String time) {
        String result = null;
        if (time.indexOf("AM") > 0) {
            result = time.substring(0, time.indexOf("AM"));
        } else {
            time = time.substring(0, time.indexOf("PM"));
            String[] starts = time.split(":");
            result = (Integer.parseInt(starts[0]) + 12) + ":" + starts[1];
        }
        return result;
    }


}
